package cn.miw.android.bdmp3.service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import cn.miw.android.bdmp3.R;
import cn.miw.android.bdmp3.model.BaiDuListItem;

/**
 * 
 * @author 蜜我 网站 http://apk.miw.cn 微博 http://www.weibo.com/askmiw 上午2:26:37
 */
public class Pub {
	public static final String PLAY_ACTION = "cn.miw.android.bdmp3.PLAY_ACTION";
	public static final String TRY_ACTION = "cn.miw.android.bdmp3.TRY_ACTION";
	public static final String PAUSE_ACTION = "cn.miw.android.bdmp3.PAUSE_ACTION";
	public static final String NEXT_ACTION = "cn.miw.android.bdmp3.NEXT_ACTION";
	public static final String PREVIOUS_ACTION = "cn.miw.android.bdmp3.PREVIOUS_ACTION";
	public static String SavePath = getRoot() + "/miwBDMP3";
	public static MediaPlayer mediaPlayer = null;
	public static int[] PlayMode = { R.drawable.mode_repeateall, R.drawable.mode_repeaterandom, R.drawable.mode_sequence };
	public static int curMode = 0;
	public static ComponentName MusicComponent;
	public static int curPos = 0;
	public static String curSongName = "";
	public static HashMap<String, BaiDuListItem> downListMap = new HashMap<String, BaiDuListItem>();
	public static List<BaiDuListItem> localMusics = null;

	public static void playMusic(Context context) {
		Intent intent = new Intent(Pub.PLAY_ACTION);
		intent.setComponent(Pub.MusicComponent);
		context.startService(intent);
	}

	public static void prevMusic(Context context) {
		Intent intent = new Intent(Pub.PREVIOUS_ACTION);
		intent.setComponent(Pub.MusicComponent);
		context.startService(intent);
	}

	public static void nextMusic(Context context) {
		Intent intent = new Intent(Pub.NEXT_ACTION);
		intent.setComponent(Pub.MusicComponent);
		context.startService(intent);
	}

	public static void tryMusic(Context context, BaiDuListItem item) {
		Intent intent = new Intent(Pub.TRY_ACTION);
		intent.putExtra("item", item);
		intent.setComponent(Pub.MusicComponent);
		context.startService(intent);
	}

	public static void stopMusic(Context context) {
		Intent intent = new Intent(Pub.PAUSE_ACTION);
		intent.setComponent(Pub.MusicComponent);
		context.startService(intent);
	}

	public static List<String> listDirs(String path, String p) {
		File file = new File(path);
		if (!file.exists()){
			file = createSDDir(path);
		}
		File[] files = file.listFiles();
		List<String> retFiles = new ArrayList<String>();
		if (!path.equals(getRoot())) {
			retFiles.add(p);
		}
		if (files != null)
			for (File f : files) {
				if (f.isDirectory())
					retFiles.add(f.getName());
			}

		return retFiles;
	}

	public static File createSDDir(String dir) {
		File dirFile = new File(getRoot() + File.separator + dir + File.separator);
		dirFile.mkdir();
		return dirFile;
	}

	public static String getRoot() {
		return Environment.getExternalStorageDirectory().getAbsolutePath();
	}
}
